<%-- 
    Document   : OtherSubpage
    Created on : May 22, 2011, 5:01:43 PM
    Author     : ToanNhan
--%>

<%@page import="java.io.IOException"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="net.sf.jasperreports.engine.JRException"%>
<%@page import="net.sf.jasperreports.engine.JRExporterParameter"%>
<%@page import="net.sf.jasperreports.engine.export.JRPdfExporter"%>
<%@page import="net.sf.jasperreports.engine.JRExporter"%>
<%@page import="net.sf.jasperreports.engine.JasperPrint"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="Util.MySqlDataAccessHelper"%>
<%@page import="java.util.HashMap"%>
<%@page import="Pojos.ThamSo"%>
<%@page import="org.hibernate.Query"%>
<%@page import="org.hibernate.cfg.Configuration"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%
            if (request.getParameter("btn") != null) {
                String reportFileName = request.getRealPath("subPages") + "\\BaoGia.jasper";
                HashMap hm = new HashMap();
                SimpleDateFormat sf = new SimpleDateFormat("dd/mm/yyyy");
                String ngayHienTai = sf.format(new Date(System.currentTimeMillis()));
                hm.put("NgayHienTai", ngayHienTai);

                try {
                    MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
                    helper.open();
                    Connection conn = helper.getConnection();
                    JasperPrint print = JasperFillManager.fillReport(reportFileName, hm, conn);
                    JRExporter exporter = new JRPdfExporter();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
                    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
                    exporter.exportReport();
                    byte[] buffer = baos.toByteArray();
                    if (buffer != null && buffer.length > 0) {
                        response.setContentType("application/pdf");
                        response.setContentLength(buffer.length);
                        response.setHeader("Content-Disposition", "attachment; filename=\"BanBaoGia.pdf\"");
                        ServletOutputStream outputStream = response.getOutputStream();
                        try {
                            outputStream.write(buffer, 0, buffer.length);
                            outputStream.flush();
                        } finally {
                            if (outputStream != null) {
                                try {
                                    outputStream.close();
                                } catch (IOException ex) {
                                }
                            }
                        }
                    }
                    helper.close();

                } catch (JRException ex) {
                }
            }
%>

<div class="box" style="width: 100%;background-color: white;">
    <div style="width: 100%; text-align: left; background-color: bisque;">
        Số lược truy cập: 
        <%
                    if (session.getAttribute("daTang") == null) {
                        SessionFactory factory = new Configuration().configure("Pojos/hibernate.cfg.xml").buildSessionFactory();
                        Session sessionHibernate = factory.openSession();
                        sessionHibernate.beginTransaction();
                        Query query = sessionHibernate.createQuery("From ThamSo");
                        ThamSo thamSo = (ThamSo) query.uniqueResult();
                        thamSo.setSoLuotTruyCap(thamSo.getSoLuotTruyCap() + 1);

                        sessionHibernate.getTransaction().commit();
                        sessionHibernate.close();
                        session.setAttribute("daTang", true);
                        session.setAttribute("soLuoc", thamSo.getSoLuotTruyCap());
                    }
                    out.print(session.getAttribute("soLuoc"));
        %>
    </div>
    <div style="width: 100%; text-align: left; background-color: bisque;">
        <center>
            <form method="POST" action="subPages/OtherSubpage.jsp">
                <input type="submit" name="btn" value="Lấy Báo Giá" style="width: 50%;"/>
            </form>
        </center>
    </div>
</div>